package com.ducquoc.examreviewdegreea1;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.Spinner;

public class SignsActivity extends Activity {

	private Spinner spinner;
	SignsPojo signs = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signs);
		
		setTitle("ExamReviewDegreeA1");

		final ArrayList<SignsPojo> arr = new ArrayList<SignsPojo>();

		// datos a mostrar
		List<SignsSocialNetwork> items = new ArrayList<SignsSocialNetwork>(15);
		items.add(new SignsSocialNetwork(getString(R.string.none),
				R.drawable.none));
		items.add(new SignsSocialNetwork(getString(R.string.ban),
				R.drawable.bbcam));
		items.add(new SignsSocialNetwork(getString(R.string.dangerous),
				R.drawable.bbnguyhiem));
		items.add(new SignsSocialNetwork(getString(R.string.direct),
				R.drawable.bbchidan));
		items.add(new SignsSocialNetwork(getString(R.string.command),
				R.drawable.bbhieulenh));

		spinner = (Spinner) findViewById(R.id.spinner);
		spinner.setAdapter(new SignsSocialNetworkSpinnerAdapter(this, items));
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> adapterView, View view,
					int position, long id) {

				String signsChoose = (String) ((SignsSocialNetwork) adapterView
						.getItemAtPosition(position)).getName();

				String signsType0 = "Chọn loại biển báo";
				String signsType1 = "Biển báo cấm";
				String signsType2 = "Biển báo nguy hiểm";
				String signsType3 = "Biển báo chỉ dẫn và biển phụ";
				String signsType4 = "Biển báo hiệu lệnh";
				
				int compare0 = signsChoose.compareTo(signsType0);
				int compare1 = signsChoose.compareTo(signsType1);
				int compare2 = signsChoose.compareTo(signsType2);
				int compare3 = signsChoose.compareTo(signsType3);
				int compare4 = signsChoose.compareTo(signsType4);

				try {
					//xoa du lieu khi chon moi.
					arr.removeAll(arr);
					
					DocumentBuilderFactory dbf = DocumentBuilderFactory
							.newInstance();
					DocumentBuilder db = dbf.newDocumentBuilder();
					Document doc = db.parse(getAssets().open(
							"list_ban_trafficsigns.xml"));
					
					if(compare1 == 0) {
						doc = db.parse(getAssets().open(
								"list_ban_trafficsigns.xml"));
					}
					else if(compare2 == 0)
					{
						doc = db.parse(getAssets().open(
								"list_dangerous_trafficsigns.xml"));
					}
					else if(compare3 == 0)
					{
						doc = db.parse(getAssets().open(
								"list_direct_auxiliary_trafficsigns.xml"));
					}
					else if(compare4 == 0)
					{
						doc = db.parse(getAssets().open(
								"list_command_trafficsigns.xml"));
					}

					// Lay ve node goc cua moi bien bao
					NodeList nodeList = doc.getElementsByTagName("signs");
					for (int i = 0; i < nodeList.getLength(); i++) {
						Node node = nodeList.item(i);
						if (node.getNodeType() == Node.ELEMENT_NODE) {
							signs = new SignsPojo();
							Element elm = (Element) node;
							// ID
							NodeList idList = elm
									.getElementsByTagName("id");
							Element idElement = (Element) idList.item(0);
							signs.setId(idElement.getTextContent().trim());

							// title
							NodeList titleList = elm
									.getElementsByTagName("title");
							Element titleElement = (Element) titleList
									.item(0);
							signs.setTitle(titleElement.getTextContent()
									.trim());

							// group
							NodeList groupList = elm
									.getElementsByTagName("group");
							Element groupElement = (Element) groupList
									.item(0);
							signs.setGroup(groupElement.getTextContent()
									.trim());

							// icon
							NodeList iconList = elm
									.getElementsByTagName("icon");
							Element iconElement = (Element) iconList
									.item(0);
							signs.setIcon(iconElement.getTextContent()
									.trim());
							arr.add(signs);
						}
					}
				} 
				catch (Exception e) {

				}
				
				if(compare0 == 0) {
					//xoa du lieu trong ArrayList
					arr.removeAll(arr);
				}

				ListView lv = (ListView) findViewById(R.id.lvTrafficSigns);
				SignsArrayAdapter myArr = new SignsArrayAdapter(
						SignsActivity.this,
						R.layout.activity_signs_custom_listview, arr);
				lv.setAdapter(myArr);
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapterView) {
				// nothing
			}
		});

	}
}